package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.PullOutBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.utils.Utils;
import com.hangzhou.welbeing.welbeinginstrument.view.CustomDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 09 月 13.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.activity
 * 工程名 welbeing_pregnant
 *
 * @desc 设置页面
 */
public class SettingActivity extends Activity {
    private static final String TAG = "SettingActivity";
    @Bind(R.id.activity_setting_iv_return)
    ImageView activitySettingIvReturn;
    @Bind(R.id.fragment_find_rl_man_near)
    RelativeLayout fragmentFindRlManNear;
    @Bind(R.id.activity_setting_rl_account_to_help_decide)
    RelativeLayout activitySettingRlAccountToHelpDecide;
    @Bind(R.id.activity_setting_rl_change_password)
    RelativeLayout activitySettingRlChangePassword;
    @Bind(R.id.activity_setting_rl_privacy_settings)
    RelativeLayout activitySettingRlPrivacySettings;
    @Bind(R.id.activity_setting_rl_new_message)
    RelativeLayout activitySettingRlNewMessage;
    @Bind(R.id.activity_setting_rl_clear_cache)
    RelativeLayout activitySettingRlClearCache;
    @Bind(R.id.activity_setting_rl_About_app)
    RelativeLayout activitySettingRlAboutApp;
    @Bind(R.id.activity_setting_rl_feed_back)
    RelativeLayout activitySettingRlFeedBack;
    @Bind(R.id.activity_setting_rl_pull_out)
    RelativeLayout activitySettingRlPullOut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);


        Utils.setStatusBar(this,false,false);
        Utils.setStatusTextColor(true,this);



    }

    @OnClick({R.id.activity_setting_iv_return, R.id.fragment_find_rl_man_near, R.id.activity_setting_rl_account_to_help_decide,
            R.id.activity_setting_rl_change_password, R.id.activity_setting_rl_privacy_settings, R.id.activity_setting_rl_new_message,
            R.id.activity_setting_rl_clear_cache,R.id.activity_setting_rl_feed_back,
            R.id.activity_setting_rl_pull_out,R.id.activity_setting_rl_About_app})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_setting_iv_return:
                finish();
                break;
            case R.id.fragment_find_rl_man_near:
                break;
            case R.id.activity_setting_rl_account_to_help_decide:
                Intent intent = new Intent(SettingActivity.this, AccountToHelpDecideActivity.class);
                startActivity(intent);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

                break;
            case R.id.activity_setting_rl_change_password:
                Intent intent1 = new Intent(SettingActivity.this, ChangePasswordActivity.class);
                startActivity(intent1);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

                break;
            case R.id.activity_setting_rl_privacy_settings:
                break;
            case R.id.activity_setting_rl_new_message:
                Intent intent2 = new Intent(SettingActivity.this, NewMessageActivity.class);
                startActivity(intent2);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

                break;
            case R.id.activity_setting_rl_clear_cache:
                dialog();
                break;
            case R.id.activity_setting_rl_feed_back: //意见反馈
                Intent intent3 = new Intent(SettingActivity.this, FeedBackActivity.class);
                startActivity(intent3);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

                break;
            case R.id.activity_setting_rl_pull_out: //退出登录
                pullOut();
            break;
            case R.id.activity_setting_rl_About_app:
                Intent intent5= new Intent(SettingActivity.this, AboutAppActivity.class);
                startActivity(intent5);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

                break;
        }
    }
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    protected void dialog() {
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        builder.setMessage("注意");
        builder.setTitle("是否清空缓存");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
               // MyToast.show(SettingActivity.this, "缓存清空！");
                Toast.makeText(SettingActivity.this,"缓存清空",Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }
    private void pullOut() {
        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id","");
        String token = sp.getString("token","");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.PULL_OUT)
                .addParams("cus_id", user_id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String parseNetworkResponse =  response.body().string();
                         Log.e(TAG,"parseNetworkResponse========"+parseNetworkResponse);
                        Gson gson =  new Gson();
                        final PullOutBean pullOutBean =  gson.fromJson(parseNetworkResponse,PullOutBean.class);
                        if(pullOutBean.errorCode == 200){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(SettingActivity.this,"您已经退出了登录",Toast.LENGTH_LONG).show();

                                    Intent intent = new Intent(SettingActivity.this,EntranceActivity.class);
                                    startActivity(intent);
                                    finish();


                                    SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
                                    SharedPreferences.Editor edit = sp.edit();
                                    edit.putString("user_id","");
                                    edit.putString("token","");
                                    edit.putBoolean("is_login",false);
                                    edit.putString("tel","");
                                    edit.putString("birthday","");
                                    edit.putString("state","");
                                    edit.putString("cus_logo","");
                                    edit.putString("qq","");
                                    edit.putString("weixin","");
                                    edit.putString("cus_logo","");
                                    edit.putString("cus_name","");
                                    edit.putString("circle_id","");
                                    edit.commit();

                                }
                            });

                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(SettingActivity.this,pullOutBean.errorMessage,Toast.LENGTH_LONG).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
}
